package orxatas.travelme.activity;

import com.markupartist.android.widget.ActionBar;

import orxatas.travelme.R;
import orxatas.travelme.databases.exceptions.PlaceNotInLocal;
import orxatas.travelme.entity.Group;
import orxatas.travelme.entity.Place;
import orxatas.travelme.entity.PlaceType;
import orxatas.travelme.manager.GroupManager;
import orxatas.travelme.manager.PlaceManager;
import orxatas.travelme.sync.AsyncNoticeCode;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class PlaceViewActivity extends Activity implements AsyncActivity {
	public static final int NORMAL_VIEW = 0;
	public static final int SEARCH_VIEW = 1;
	public static final int SEARCH_NEAR_VIEW = 2;
	
	private static final String TAG = "Place View Activity";
	private boolean progressBarVisibility = false;
	private final static int PLACE_PICK = 5;
	
	private Place place;
		
	private ActionBar actionBar;
	
	/**
	 * The loading - dialog.
	 * */
	private ProgressDialog dialog = null;
	
	/**
	 * Place Manager
	 * */
	private PlaceManager placeManager;
	
	/**
	 * Group Manager
	 * */
	private GroupManager groupManager;
	
	/**
	 * Place id
	 * */
	private int placeId = -1;
	
	private int groupId = -1;
	
	private int typeView;
	
	private Group group;
	
	/**
	 * TextView description.
	 * */
	private TextView descriptionTV, longLocationTV, typeTV;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.placedetail);
		
		placeId = getIntent().getIntExtra("place_id", -1);
		groupId = getIntent().getIntExtra("group_id", -1);
		typeView = getIntent().getIntExtra("type_view", NORMAL_VIEW);
		
		actionBar = (ActionBar) findViewById(R.id.actionbar);
		
		descriptionTV = (TextView) findViewById(R.id.placedetail_description);
		longLocationTV = (TextView) findViewById(R.id.placedetail_longlocation);
		typeTV = (TextView) findViewById(R.id.placedetail_typevalue);
		
		
		if(placeId == -1) {
			Log.e(TAG, "Fallo al iniciar la actividad. No se especifico el id de lugar.");
			finish();
		}
		
		placeManager = new PlaceManager(this);
		
		if(groupId != -1){
			groupManager = new GroupManager(this);
			group = groupManager.getGroup(groupId);
			
			if(group != null){
				actionBar.addAction(new ActionBar.Action() {
					
					/*
					 * Acción que se realizará cuando se haga "click" en el icono de la acción.
					 * */
					@Override
					public void performAction(View view) {
						if(place != null && typeView == NORMAL_VIEW){
							Intent intent = new Intent(PlaceViewActivity.this, NewEntryActivity.class);
							intent.putExtra("idgroup", groupId);
							intent.putExtra("idplace", placeId);
							startActivity(intent);
						}
					}
					
					/*
					 * Debe devolver el identificador del icono que se mostrará.
					 * */
					@Override
					public int getDrawable() {
						return R.drawable.ic_placeview_newentry;
					}
				});
			}
		}
		
		getPlace();
		
	}
	
	
	private void setActionBarTitle(String namePlace, String nameGroup){
		actionBar.setTitle(namePlace+nameGroup, getAssets(), "Ubuntu-M.ttf" );		
	}
	
	private void getPlace(){
		try {
			place = placeManager.getPlace(placeId);
			String namePlace = place.getName();
			String nameGroup = (group != null)? " ("+group.getName()+")": "";
			
			setActionBarTitle(namePlace, nameGroup);
			
			Spanned description = Html.fromHtml(place.getDescription());
			String longLocation = place.getLongLocation();
			String type = getPlaceType(place.getPlaceType());
			if(description != null) descriptionTV.setText(description);	
			if(longLocation != null) longLocationTV.setText(longLocation);
			if(type != null) typeTV.setText(" "+type);
		} catch (PlaceNotInLocal e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	
	private String getPlaceType(PlaceType placeType){
		switch(placeType){
			case CITY:
				return getResources().getString(R.string.placedetail_type_city);
			case REGION:
				return getResources().getString(R.string.placedetail_type_region);
			case COUNTRY:
				return getResources().getString(R.string.placedetail_type_country);
			case CONTINENT:
				return getResources().getString(R.string.placedetail_type_continent);
			case NATURAL:
				return getResources().getString(R.string.placedetail_type_natural);
			case CULTURAL:
				return getResources().getString(R.string.placedetail_type_cultural);
			case POI:
				return getResources().getString(R.string.placedetail_type_poi);
			case INFRASTRUCTURE:
				return getResources().getString(R.string.placedetail_type_infrastructure);
			case HOTEL:
				return getResources().getString(R.string.placedetail_type_hotel);
			case RESTAURANT:
				return getResources().getString(R.string.placedetail_type_restaurant);
			case PUB:
				return getResources().getString(R.string.placedetail_type_pub);
			case DISCO:
				return getResources().getString(R.string.placedetail_type_disco);
			default:		
		}
		return null;
		
	}
	
	/**
	 * Example of HOWTO manage this.
	 * This will do something if your activity require it. If not, do nothing.
	 * */
	@Override
	public void asyncNotice(AsyncNoticeCode code) {
		switch(code){
			case PLACES_CHANGED:
				getPlace();
				break;
			default:
		}
	}

	@Override
	public Activity getActivity() {
		return this;
	}

	@Override
	public void syncStarted() {
		actionBar.setProgressBarVisibility(View.VISIBLE);
	}

	@Override
	public void syncEnded() {
		actionBar.setProgressBarVisibility(View.GONE);
	}

	@Override
	public void syncAndWaitCallStarted() {
		if(dialog == null) {
			dialog = ProgressDialog.show(this, "", "Loading. Please wait...", true);
			dialog.setCancelable(false);
		}
	}

	@Override
	public void syncAndWaitCallEnded(Object o, int syncAction) {
		if(dialog != null) {
			dialog.cancel();
			dialog = null;
		}		
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflator = null;
		switch(typeView){
			case NORMAL_VIEW:
				inflator = getMenuInflater();
				inflator.inflate(R.layout.menu_place_view, menu);
				break;
			case SEARCH_VIEW:
			case SEARCH_NEAR_VIEW:
				inflator = getMenuInflater();
				inflator.inflate(R.layout.menu_map, menu);
				break;
			default:
				break;
		}
		return true;
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent MostrarIntent = null;
		
		 switch (item.getItemId()) {
		    case R.id.mostrar_mapa:
		    	switch(typeView){
					case NORMAL_VIEW:
				    	MostrarIntent = new Intent(PlaceViewActivity.this, MapPlacesActivity.class);
				    	MostrarIntent.putExtra("id_group", groupId);
				    	MostrarIntent.putExtra("id_place", placeId);
				    	startActivity(MostrarIntent);
						break;
					case SEARCH_VIEW:
					case SEARCH_NEAR_VIEW:
						MostrarIntent = new Intent(PlaceViewActivity.this, MapPlaceActivity.class);
				    	MostrarIntent.putExtra("id_place", placeId);
				    	startActivity(MostrarIntent);
						break;
					default:
						break;
		    	}
		    	break;
		    case R.id.search_near:
		    	MostrarIntent = new Intent(PlaceViewActivity.this, PlaceSearchNearActivity.class);
		    	MostrarIntent.putExtra("id_group", groupId);
		    	MostrarIntent.putExtra("id_place", placeId);
		    	MostrarIntent.putExtra("returns", true);
		    	startActivityForResult(MostrarIntent, PLACE_PICK);
		    	break;
		 }
		 return super.onOptionsItemSelected(item);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch(requestCode){
		case PLACE_PICK:
			if(data !=null){
				int newPlaceId = data.getIntExtra("place_id", -1);
				if(newPlaceId != -1) {
					PlaceViewActivity.this.setResult(RESULT_OK, data);
					finish();
				}
			}
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}	
	
}
